<?php
/**
* Api_View_Helper_UserHelper class for Boomerang Referral Network
*
* Api_View_Helper_UserHelper is the view helper class which is used to provide
* multiple functionalities at view pages related to User Management
* 
* @package      Api_View_Helper
* @copyright    Copyright (c) 2011-2012 Boomerang Referral Network
* @version      $Id: UserHelper.php v 1.0.0$
* @since        Class available since Release 1.0.0
* @author       Anindya Roy Chodhury <anindya23@gmail.com>
*/
class Api_View_Helper_UserHelper extends Zend_View_Helper_Abstract
{
    /**
     * Api_View_Helper_UserHelper instance for Singleton
     * 
     * @static
     * @var Api_View_Helper_UserHelper 
     */
    private static $_instance = null;

    /**
     * Get Doctrine Entity Manager from Zend_Registry
     * 
     * @return Doctrine\ORM\EntityManager 
     */
    public function getEntityManager()
    {
        return Zend_Registry::get('doctrine')->getEntityManager();
    }

    /**
     * Get Api_View_Helper_UserHelper instance using Singleton
     * 
     * @static
     * @return Api_View_Helper_UserHelper 
     */
    public function userHelper()
    {
        return $this;
    }
    
    /**
     * Get Api_View_Helper_UserHelper instance using Singleton
     * 
     * @static
     * @return Api_View_Helper_UserHelper 
     */
    public static function getInstance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * Get full name of the user
     * 
     * @param Api\Core\Entity\User $user
     * @return string 
     */
    public function getNameByUser($user = null)
    {
        $em = $this->getEntityManager();
        if (!is_null($user)) {
            if (!($user instanceof Api\Core\Entity\EntityAbstract)) {
                $user = $em->getRepository('Api\Core\Entity\User', $user);
            }
        } else {
            $userBll = new Api_Core_Bll_User_User();
            $user = $userBll->getLoggedInUser();
        }
        return $user->firstName . " " . $user->lastName;
    }

    /**
     * Check user is already logged in or not
     * @return boolean 
     */
    public function isLoggedIn()
    {
        $status = false;
        $userSession = new Zend_Session_Namespace('identity');
        if ($userSession->username) {
            $status = true;
        }
        return $status;
    }

    /**
     * Get user profile picture path
     * 
     * @param Api\Core\Entity\User $user
     * @return string 
     */
    public function getUserImage($user = null)
    {
        $userBll = new Api_Core_Bll_User_User();
        if (!is_null($user)) {
            if (!($user instanceof Api\Core\Entity\EntityAbstract)) {
                $user = $this->getEntityManager()->getRepository('Api\Core\Entity\User', $user);
            }
        } else {
            $user = $userBll->getLoggedInUser();
        }
        return $userBll->getProfileImg($user);
    }
    
}